package $2_2coreclasses;

import java.util.StringJoiner;

public class FnStringJoiner {

    public static void main(String[] args) {
        // StringJoiner
        String[] names = {"Bob", "Alice", "Grace"};
        var sj = new StringJoiner(", ", "Hello ", "!");
        for (String name : names) {
            sj.add(name);
        }
        System.out.println(sj.toString());
        
        var s = String.join(", ", names);
        System.out.println(s);
        
        // 请使用StringJoiner构造一个SELECT语句：
        
        String[] fields = { "name", "position", "salary" };
        String table = "employee";
        String select = buildSelectSql(table, fields);
        System.out.println(select);
        System.out.println("SELECT name, position, salary FROM employee".equals(select) ? "测试成功" : "测试失败");
    }
    static String buildSelectSql(String table, String[] fields) {
        // TODO: 用了两个 StringJoiner
        var vfields = new StringJoiner(", ");
        for(String field : fields) {
            vfields.add(field);
        }
        // var field = String.join(", ", fields).toString();
        
        var strrtn = new StringJoiner(" ", "SELECT ","");
        strrtn.add(vfields.toString());
        strrtn.add("FROM");
        strrtn.add(table);
        return strrtn.toString();
    }
}
